package com.voute.Presenter.Impl;


import com.voute.Model.Bean.BaseBean;
import com.voute.Model.Bean.DiscoveryBean;
import com.voute.Model.IDiscoveryModel;
import com.voute.Model.Impl.DiscoveryImpl;
import com.voute.Presenter.IDiscoveryPresenter;
import com.voute.Utils.CommonSubscriber;
import com.voute.Utils.TransformUtils;
import com.voute.View.IView.IDiscoveryView;

import rx.functions.Action0;

/**
 * 热门presenter的实现类
 * Created by liukanshan on 2016/12/8.
 */

public class DiscoveryPresenterImpl implements IDiscoveryPresenter {

    private IDiscoveryModel iDiscoveryModelImpl;
    private IDiscoveryView iDiscoveryView;

    public DiscoveryPresenterImpl(IDiscoveryView view) {
        iDiscoveryModelImpl = new DiscoveryImpl();
        iDiscoveryView = view;
    }

    @Override
    public void getDiscovery(String anchor, boolean circle) {
        if (circle) {
            iDiscoveryModelImpl.getDiscoveryFeeds(anchor).compose(TransformUtils.<DiscoveryBean>defaultSchedulers()).doOnSubscribe(new Action0() {
                @Override
                public void call() {
                    iDiscoveryView.showLoading();
                }
            }).subscribe(new CommonSubscriber<DiscoveryBean>() {
                @Override
                public void onSuccess(DiscoveryBean response) {
                    iDiscoveryView.hideLoading();
                    iDiscoveryView.setDiscovery(response);
                }

                @Override
                public void onFailure(BaseBean response) {
                    iDiscoveryView.hideLoading();
                    iDiscoveryView.showError(response.getMsg());
                }
            });
        } else {
            iDiscoveryModelImpl.getLocalFeeds(anchor).compose(TransformUtils.<DiscoveryBean>defaultSchedulers()).doOnSubscribe(new Action0() {
                @Override
                public void call() {
                    iDiscoveryView.showLoading();
                }
            }).subscribe(new CommonSubscriber<DiscoveryBean>() {
                @Override
                public void onSuccess(DiscoveryBean response) {
                    iDiscoveryView.hideLoading();
                    iDiscoveryView.setDiscovery(response);
                }

                @Override
                public void onFailure(BaseBean response) {
                    iDiscoveryView.hideLoading();
                    iDiscoveryView.showError(response.getMsg());
                }
            });
        }
    }
}
